<template>
  <div class="drawer">
    <el-drawer
      v-model="isVisible"
      :size="layout.size ?? '35%'"
      :direction="layout.direction ?? 'rtl'"
      :show-close="layout.showClose ?? true"
      :close-on-click-modal="layout.closeOnClickModal ?? true"
      @close="cancelClick"
    >
      <template #header>
        <div class="header">
          <div class="line"></div>
          <div class="title">{{ layout.title ?? '默认标题' }}</div>
        </div>
      </template>
      <template #default>
        <ly-form
          ref="formRef"
          :page-name="pageName"
          :is-edit-btn="isEditBtn"
          :form="form"
        >
          <template
            v-for="item in form.items"
            :key="item.prop"
            v-slot:[item.slotName]="formItem"
          >
            <slot
              v-if="item.slotName"
              v-bind="formItem"
              :name="item.slotName"
            ></slot>
          </template>
        </ly-form>
      </template>
      <template #footer>
        <div class="footer">
          <el-button size="default" @click="cancelClick">取消</el-button>
          <el-button size="default" type="primary" @click="confirmClick">
            确定
          </el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
import LyForm from '@/base-ui/lyform/ly-from.vue'
import useModal from '@/hooks/use-modal'
import type { IDialogAndDrawerLayout, ILyForm } from '@/global/types'

interface IProps {
  isShowModal: boolean
  pageName?: string
  layout: IDialogAndDrawerLayout
  form: ILyForm
  isEditBtn?: boolean
}

const props = withDefaults(defineProps<IProps>(), {
  isEditBtn: false,
  isShowModal: false
})
const emits = defineEmits(['cancelClick', 'confirmClick'])
const { isVisible, formRef, cancelClick, confirmClick } = useModal(props, emits)
</script>

<style scoped lang="less">
.drawer {
  .header {
    display: flex;

    .line {
      width: 3px;
      height: 16px;
      margin-right: 10px;
      background-color: var(--ly-primary-color);
    }

    .title {
      height: 16px;
      line-height: 16px;
      font-size: 14px;
    }
  }

  .footer {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

:deep(.el-drawer__footer) {
  text-align: center;
}
</style>
